﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraTab;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using Extra;
    using HRM.CORE;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using Extra.Data.Extra.Forms;

    public class xfmShiftAdd : XtraForm
    {
        private SimpleButton btCancel;
        private SimpleButton btCreate;
        private SimpleButton btDelete;
        private Common.Class.clsAllOption clsAllOption = new Common.Class.clsAllOption();
        private IContainer components = null;
        private TimeEdit dtYear;
        private DevExpress.Utils.ImageCollection imageCollection1;
        private HRM_TIMEKEEPER_TABLELIST l_TimeKeeperTableList = new HRM_TIMEKEEPER_TABLELIST();
        private LabelControl labelControl1;
        private ImageListBoxControl lbNoTimeKeeperShiftList;
        private ImageListBoxControl lbTimeKeeperShiftList;
        private LabelControl lcDescription;
        private int m_Month = 0;
        private int m_Year = 0;
        private PanelControl panelControl1;
        private XtraTabControl xtraTabControl1;
        private XtraTabPage xtraTabPage1;
        private XtraTabPage xtraTabPage2;

        public event ClosedHandler Closed;

        public event TimekeeperTableDeletedHandler TimekeeperTableDeleted;

        public event TimekeeperTableInsertedHandler TimekeeperTableInserted;

        public event UnShiftDataHandler UnShiftData;

        public xfmShiftAdd()
        {
            this.InitializeComponent();
            this.dtYear.EditValue = this.clsAllOption.MonthDefault;
            this.LoadTimeKeeperShiftList();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            string str = (this.lbNoTimeKeeperShiftList.SelectedItem as ImageListBoxItem).Value.ToString();
            char[] separator = new char[] { ' ', '-' };
            string[] strArray = str.Split(separator);
            this.m_Month = Convert.ToInt32(strArray[1].ToString());
            this.m_Year = Convert.ToInt32(strArray[2].ToString());
            this.RaiseUnShiftDataHandler(this.m_Month, this.m_Year);
            base.Close();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            XfmBackupDatabase.ClosedHandler handler = null;
            string str = (this.lbTimeKeeperShiftList.SelectedItem as ImageListBoxItem).Value.ToString();
            char[] separator = new char[] { ' ', '-' };
            string[] strArray = str.Split(separator);
            this.m_Month = Convert.ToInt32(strArray[1].ToString());
            this.m_Year = Convert.ToInt32(strArray[2].ToString());
            if (XtraMessageBox.Show("Bạn c\x00f3 muốn sao lưu dữ liệu lại trước khi thực hiện chức năng n\x00e0y kh\x00f4ng?", "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                XfmBackupDatabase database = new XfmBackupDatabase();
                if (handler == null)
                {
                    handler = s => this.Delete();
                }
                database.Closed += handler;
                database.ShowDialog();
            }
            else
            {
                this.Delete();
            }
        }

        private void Delete()
        {
            if (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?\n\nDữ liệu bị x\x00f3a bao gồm:\n\n\t- Dữ liệu t\x00ednh lương, lương tăng ca trong th\x00e1ng.\n\t- Dữ liệu chấm c\x00f4ng tăng ca.\n\t- Dữ liệu chấm c\x00f4ng chi tiết theo ng\x00e0y, chấm c\x00f4ng th\x00e1ng.\n\t- Dữ liệu xếp ca chi tiết của th\x00e1ng được chọn.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                if (this.l_TimeKeeperTableList.Delete(this.m_Month, this.m_Year) == "OK")
                {
                    XtraMessageBox.Show("Đ\x00e3 ho\x00e0n tất việc x\x00f3a dữ liệu!", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.RaiseTimekeeperTableDeletedHander();
                    base.Close();
                }
                else
                {
                    XtraMessageBox.Show("Dữ liệu n\x00e0y kh\x00f4ng thể x\x00f3a được!", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void dtYear_EditValueChanged(object sender, EventArgs e)
        {
            this.LoadTimeKeeperShiftList();
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xfmShiftAdd));
            this.btCreate = new SimpleButton();
            this.imageCollection1 = new DevExpress.Utils.ImageCollection(this.components);
            this.btCancel = new SimpleButton();
            this.lcDescription = new LabelControl();
            this.panelControl1 = new PanelControl();
            this.btDelete = new SimpleButton();
            this.dtYear = new TimeEdit();
            this.labelControl1 = new LabelControl();
            this.xtraTabControl1 = new XtraTabControl();
            this.xtraTabPage1 = new XtraTabPage();
            this.lbNoTimeKeeperShiftList = new ImageListBoxControl();
            this.xtraTabPage2 = new XtraTabPage();
            this.lbTimeKeeperShiftList = new ImageListBoxControl();
            this.imageCollection1.BeginInit();
            this.panelControl1.BeginInit();
            this.panelControl1.SuspendLayout();
            this.dtYear.Properties.BeginInit();
            this.xtraTabControl1.BeginInit();
            this.xtraTabControl1.SuspendLayout();
            this.xtraTabPage1.SuspendLayout();
            ((ISupportInitialize) this.lbNoTimeKeeperShiftList).BeginInit();
            this.xtraTabPage2.SuspendLayout();
            ((ISupportInitialize) this.lbTimeKeeperShiftList).BeginInit();
            base.SuspendLayout();
            this.btCreate.ImageIndex = 2;
            this.btCreate.ImageList = this.imageCollection1;
            this.btCreate.Location = new Point(0x57, 10);
            this.btCreate.Name = "btCreate";
            this.btCreate.Size = new Size(0x6a, 0x17);
            this.btCreate.TabIndex = 2;
            this.btCreate.Text = "Ph\x00e1t Sinh (>>)";
            this.btCreate.Click += new EventHandler(this.btCreate_Click);
            this.imageCollection1.ImageStream = (ImageCollectionStreamer) manager.GetObject("imageCollection1.ImageStream");
            this.imageCollection1.Images.SetKeyName(0, "ico_alpha_Search_16x16.png");
            this.imageCollection1.Images.SetKeyName(1, "ico_alpha_Delete_16x16.png");
            this.imageCollection1.Images.SetKeyName(2, "document2.png");
            this.imageCollection1.Images.SetKeyName(3, "open-16x16.png");
            this.imageCollection1.Images.SetKeyName(4, "close.png");
            this.btCancel.ImageIndex = 4;
            this.btCancel.ImageList = this.imageCollection1;
            this.btCancel.Location = new Point(0x169, 10);
            this.btCancel.Name = "btCancel";
            this.btCancel.Size = new Size(0x62, 0x17);
            this.btCancel.TabIndex = 3;
            this.btCancel.Text = "Tho\x00e1t";
            this.btCancel.Click += new EventHandler(this.btCancel_Click);
            this.lcDescription.Appearance.Options.UseTextOptions = true;
            this.lcDescription.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
            this.lcDescription.AutoSizeMode = LabelAutoSizeMode.None;
            this.lcDescription.Location = new Point(0x11, 5);
            this.lcDescription.Name = "lcDescription";
            this.lcDescription.Size = new Size(0x1be, 0x31);
            this.lcDescription.TabIndex = 4;
            this.lcDescription.Text = manager.GetString("lcDescription.Text");
            this.panelControl1.Appearance.BackColor = Color.Transparent;
            this.panelControl1.Appearance.Options.UseBackColor = true;
            this.panelControl1.BorderStyle = BorderStyles.NoBorder;
            this.panelControl1.Controls.Add(this.btDelete);
            this.panelControl1.Controls.Add(this.btCancel);
            this.panelControl1.Controls.Add(this.btCreate);
            this.panelControl1.Dock = DockStyle.Bottom;
            this.panelControl1.Location = new Point(0, 0x124);
            this.panelControl1.Name = "panelControl1";
            this.panelControl1.Size = new Size(0x1db, 0x2b);
            this.panelControl1.TabIndex = 6;
            this.btDelete.Enabled = false;
            this.btDelete.ImageIndex = 1;
            this.btDelete.ImageList = this.imageCollection1;
            this.btDelete.Location = new Point(0xc7, 10);
            this.btDelete.Name = "btDelete";
            this.btDelete.Size = new Size(0x93, 0x17);
            this.btDelete.TabIndex = 4;
            this.btDelete.Text = "X\x00f3a To\x00e0n Bộ Dữ Liệu (X)";
            this.btDelete.Click += new EventHandler(this.btDelete_Click);
            this.dtYear.EditValue = new System.DateTime(0x7dc, 5, 0x1d, 0, 0, 0, 0);
            this.dtYear.Location = new Point(0x5d, 0x39);
            this.dtYear.Name = "dtYear";
            this.dtYear.Properties.Appearance.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.dtYear.Properties.Appearance.Options.UseFont = true;
            this.dtYear.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton() });
            this.dtYear.Properties.Mask.EditMask = "yyyy";
            this.dtYear.Size = new Size(0x40, 20);
            this.dtYear.TabIndex = 8;
            this.dtYear.EditValueChanged += new EventHandler(this.dtYear_EditValueChanged);
            this.labelControl1.Location = new Point(0x23, 60);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new Size(0x34, 13);
            this.labelControl1.TabIndex = 9;
            this.labelControl1.Text = "Chọn năm:";
            this.xtraTabControl1.Location = new Point(12, 0x56);
            this.xtraTabControl1.Name = "xtraTabControl1";
            this.xtraTabControl1.SelectedTabPage = this.xtraTabPage1;
            this.xtraTabControl1.Size = new Size(0x1c8, 210);
            this.xtraTabControl1.TabIndex = 10;
            this.xtraTabControl1.TabPages.AddRange(new XtraTabPage[] { this.xtraTabPage1, this.xtraTabPage2 });
            this.xtraTabControl1.Selected += new TabPageEventHandler(this.xtraTabControl1_Selected);
            this.xtraTabPage1.Controls.Add(this.lbNoTimeKeeperShiftList);
            this.xtraTabPage1.Name = "xtraTabPage1";
            this.xtraTabPage1.Size = new Size(0x1c1, 0xb5);
            this.xtraTabPage1.Text = "Th\x00e1ng Chưa Ph\x00e1t Sinh";
            this.lbNoTimeKeeperShiftList.Dock = DockStyle.Fill;
            this.lbNoTimeKeeperShiftList.HighlightedItemStyle = HighlightStyle.Skinned;
            this.lbNoTimeKeeperShiftList.ImageList = this.imageCollection1;
            this.lbNoTimeKeeperShiftList.Location = new Point(0, 0);
            this.lbNoTimeKeeperShiftList.Name = "lbNoTimeKeeperShiftList";
            this.lbNoTimeKeeperShiftList.Size = new Size(0x1c1, 0xb5);
            this.lbNoTimeKeeperShiftList.TabIndex = 9;
            this.xtraTabPage2.Controls.Add(this.lbTimeKeeperShiftList);
            this.xtraTabPage2.Name = "xtraTabPage2";
            this.xtraTabPage2.Size = new Size(0x1c1, 0xb5);
            this.xtraTabPage2.Text = "Th\x00e1ng Đ\x00e3 Ph\x00e1t Sinh";
            this.lbTimeKeeperShiftList.Dock = DockStyle.Fill;
            this.lbTimeKeeperShiftList.HighlightedItemStyle = HighlightStyle.Skinned;
            this.lbTimeKeeperShiftList.ImageList = this.imageCollection1;
            this.lbTimeKeeperShiftList.Location = new Point(0, 0);
            this.lbTimeKeeperShiftList.Name = "lbTimeKeeperShiftList";
            this.lbTimeKeeperShiftList.Size = new Size(0x1c1, 0xb5);
            this.lbTimeKeeperShiftList.TabIndex = 8;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x1db, 0x14f);
            base.Controls.Add(this.xtraTabControl1);
            base.Controls.Add(this.labelControl1);
            base.Controls.Add(this.dtYear);
            base.Controls.Add(this.panelControl1);
            base.Controls.Add(this.lcDescription);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "xfmShiftAdd";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Danh s\x00e1ch bảng chấm c\x00f4ng th\x00e1ng";
            this.imageCollection1.EndInit();
            this.panelControl1.EndInit();
            this.panelControl1.ResumeLayout(false);
            this.dtYear.Properties.EndInit();
            this.xtraTabControl1.EndInit();
            this.xtraTabControl1.ResumeLayout(false);
            this.xtraTabPage1.ResumeLayout(false);
            ((ISupportInitialize) this.lbNoTimeKeeperShiftList).EndInit();
            this.xtraTabPage2.ResumeLayout(false);
            ((ISupportInitialize) this.lbTimeKeeperShiftList).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void LoadTimeKeeperShiftList()
        {
            this.lbNoTimeKeeperShiftList.Items.Clear();
            this.lbTimeKeeperShiftList.Items.Clear();
            DataTable listByYear = this.l_TimeKeeperTableList.GetListByYear(int.Parse(this.dtYear.Text));
            foreach (DataRow row in listByYear.Rows)
            {
                this.lbTimeKeeperShiftList.Items.Add(row["TimeKeeperTableListName"].ToString(), 2);
            }
            for (int i = 1; i <= 12; i++)
            {
                if (!this.l_TimeKeeperTableList.Exist(i, int.Parse(this.dtYear.Text)))
                {
                    this.lbNoTimeKeeperShiftList.Items.Add("Th\x00e1ng " + i.ToString() + "-" + this.dtYear.Text, 2);
                }
            }
        }

        private void RaiseClosedHandler()
        {
            if (this.Closed != null)
            {
                this.Closed(this);
            }
        }

        private void RaiseTimekeeperTableDeletedHander()
        {
            if (this.TimekeeperTableDeleted != null)
            {
                this.TimekeeperTableDeleted(this);
            }
        }

        private void RaiseTimekeeperTableInsertedHander()
        {
            if (this.TimekeeperTableInserted != null)
            {
                this.TimekeeperTableInserted(this);
            }
        }

        private void RaiseUnShiftDataHandler(int Month, int Year)
        {
            if (this.UnShiftData != null)
            {
                this.UnShiftData(this, Month, Year);
            }
        }

        private void xtraTabControl1_Selected(object sender, TabPageEventArgs e)
        {
            if (e.PageIndex == 0)
            {
                if (this.lbNoTimeKeeperShiftList.Items.Count > 0)
                {
                    this.btCreate.Enabled = true;
                }
                else
                {
                    this.btCreate.Enabled = false;
                }
                this.btDelete.Enabled = false;
            }
            else
            {
                if (this.lbTimeKeeperShiftList.Items.Count > 0)
                {
                    this.btDelete.Enabled = true;
                }
                else
                {
                    this.btDelete.Enabled = false;
                }
                this.btCreate.Enabled = false;
            }
        }

        public delegate void ClosedHandler(object sender);

        public delegate void TimekeeperTableDeletedHandler(object sender);

        public delegate void TimekeeperTableInsertedHandler(object sender);

        public delegate void UnShiftDataHandler(object sender, int Month, int Year);
    }
}

